今天要來學習的是陣列操作,包含some()
, every()
, find()
, findIndex()
, splice()
const people = [
{ name: 'Wes', year: 1988 },
{ name: 'Kait', year: 1986 },
{ name: 'Irv', year: 1970 },
{ name: 'Lux', year: 2015 }
];
const comments = [
{ text: 'Love this!', id: 523423 },
{ text: 'Super good', id: 823423 },
{ text: 'You are the best', id: 2039842 },
{ text: 'Ramen is my fav food ever', id: 123523 },
{ text: 'Nice Nice Nice!', id: 542328 }
];
//複雜
const isAdult = people.some(function (person) {
const currentYear = (new Date()).getFullYear();
if (currentYear - person.year >= 19) {
return true;
}
});
//簡化
const is_adult = people.some(persion => {
const currentYear = (new Date()).getFullYear();
return currentYear - persion.year >= 19;
});
//極簡
const is_adult = people.some(persion => (new Date()).getFullYear() - persion.year >= 19);
Array.prototype.some()
透過給定函式、測試陣列中是否至少有一個元素,通過該函式所實作的測試,這方法回傳的是布林值
const is_adult = people.every(persion => (new Date()).getFullYear() - persion.year >= 19);
Array.prototype.every()
測試陣列中的所有元素是否都通過了由給定之函式所實作的測試,這方法回傳的是布林值
const comment = comments.find(function (comment) {
if(comment.id === 823423) {
return true;
}
});
//極簡
const comment = comments.find(comment => comment.id === 823423);
Array.prototype.find()
回傳第一個滿足所提供之測試函式的元素值
// Find the comment with this ID
// delete the comment with the ID of 823423
const index = comments.findIndex(comment => comment.id === 823423);
Array.prototype.findIndex()
依據提供的測試函式,尋找陣列中符合的元素,並返回其 index(索引)
移除陣列內容
comments.splice(index, 1);
splice()
可以藉由刪除既有元素並/或加入新元素來改變一個陣列的內容
//redux
const newComments = [
...comments.slice(0, index),
...comments.slice(index + 1)
];
這是另外一種方法,是透過建立一個新陣列達成